package org.zhi.scala03

import scala.io.Source

/**
 * @className: ScalaDemo1
 * @author: Administrator
 * @date: 2025-02-26 18:12
 */

object ScalaDemo2 {
//集合
  def main(args: Array[String]): Unit = {
    val filePath  = "src/main/resources/input.txt"
    try {
      //读取文件
      val content = Source.fromFile(filePath).mkString
      //分割单词，
      val words = content.split("\\W+").filter(_.nonEmpty)
      //分组
      val map = words.groupBy(identity)
      //统计每组的values的个数返回map
      val map1 = map.mapValues(_.length)
      //排序
      val list = map1.toList.sortBy(-_._2)
      list.foreach(println)

    } catch {
      case e: Exception => println(s"错误: ${e.getMessage}")  // 异常处理‌:ml-citation{ref="1,3" data="citationList"}
    }
  }
}
